package com.gpay.erp.web.control;

import com.gpay.erp.core.system.bean.User;
import com.gpay.erp.services.system.impl.UserService;
import com.gpay.erp.web.util.WebUtil;
import com.gpay.erp.web.vo.ResponseInfo;
import com.gpay.erp.web.vo.UserSessionInfor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/")
public class UserController extends BaseController{
    private Log log = LogFactory.getLog(UserController.class);
    @Autowired
    private UserService userService;

    @RequestMapping("testJson")
    public void testJson(HttpServletRequest request, HttpServletResponse response){
        User user = new User();
        user.setUsername("admin");
        user.setPassword("123456");
        WebUtil.responseToJson(response, user);
//        RequestContext requestContext = new RequestContext(request);
//        ResponseInfo responseInfo = new ResponseInfo();
//        responseInfo.setResult("00000");
//        responseInfo.setMessage(requestContext.getMessage("user.username.again"));
////        PrintWriter out = response.getWriter();
////        out.print(responseInfo.toString());
//        WebUtil.responseToJson(response, responseInfo);

    }
    @RequestMapping("index")
    public String index(HttpServletRequest request, HttpServletResponse response){
        return "index";

    }
    @RequestMapping(value = "login")
    public String login(@ModelAttribute("userSessionInfor") UserSessionInfor userSessionInfor,HttpServletRequest request, HttpServletResponse response)
    {

        RequestContext requestContext = new RequestContext(request);

        ResponseInfo responseInfo = new ResponseInfo();
        HttpSession session = request.getSession();
        if((userSessionInfor == null)|| (userSessionInfor.getUsername() == ""))
        {
            responseInfo.setResult("1");
            responseInfo.setMessage(requestContext.getMessage("user.username.validate.null",new Object[]{requestContext.getMessage("user.username")}));
            session.setAttribute("responseInfo",responseInfo);
            return "index";
        }else if(userSessionInfor.getPasswd() == "")
        {
            responseInfo.setResult("1");
            responseInfo.setMessage(requestContext.getMessage("user.passwd.validate.null",new Object[]{userSessionInfor.getPasswd()}));
            session.setAttribute("responseInfo",responseInfo);
            return "index";
        }
        User user = new User();
        if(!"".equals(userSessionInfor.getPasswd()))
        {
            user.setUsername(userSessionInfor.getUsername());
            user.setPassword(userSessionInfor.getPasswd());
        }
        User user1 =userService.login(user);
        log.info("username:"+user1.getUsername());
        if (user1 != null)
        {
            responseInfo.setResult("0");
            responseInfo.setMessage(userSessionInfor.getUsername());
            session.setAttribute("responseInfo",responseInfo);
            return "success";
        } else {
            responseInfo.setResult("1");
            responseInfo.setMessage(requestContext.getMessage("username.or.password.mistake"));
            session.setAttribute("responseInfo",responseInfo);

            return "index";
        }
    }


    @RequestMapping("getJsonValue")
    public void getJsonValue(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RequestContext requestContext = new RequestContext(request);
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setResult("00000");
        responseInfo.setMessage(requestContext.getMessage("user.username.again"));
//        PrintWriter out = response.getWriter();
//        out.print(responseInfo.toString());
         WebUtil.responseToJson(response, responseInfo);
    }

}